// 获取图片地址信息
export function getImageInfo(src) {
	return new Promise((resolve, reject) => {
		uni.getImageInfo({
			src,
			success: (res) => {
				if (res.path) {
					resolve(res.path);
				} else {
					resolve(res.errMsg);
				}
			},
			fail: (res) => {
				reject(res);
			},
		});
	});
}

// 画字
export function circleText(
	ctx,
	text,
	x,
	y,
	size = 18,
	color = '#fff'
) {
	ctx.setFontSize(size);
	ctx.fillStyle = color;
	ctx.setTextAlign('center');
	ctx.fillText(text, x, y);
}

// 画图形
export function circleImgTwo(
	ctx,
	img,
	x,
	y,
	w,
	h,
	r,
	color = '#ffffff',
	lineWidth = 10
) {
	ctx.save();
	// 画一个图形
	if (w < 2 * r) r = w / 2;
	if (h < 2 * r) r = h / 2;
	ctx.beginPath();
	ctx.moveTo(x + r, y);
	ctx.arcTo(x + w, y, x + w, y + h, r);
	ctx.arcTo(x + w, y + h, x, y + h, r);
	ctx.arcTo(x, y + h, x, y, r);
	ctx.arcTo(x, y, x + w, y, r);
	ctx.closePath();
	if (lineWidth > 0) {
		ctx.setLineWidth(lineWidth);
	}
	ctx.strokeStyle = color; // 设置绘制圆形边框的颜色
	ctx.stroke();
	ctx.clip();
	ctx.drawImage(img, x, y, w, h);
	ctx.restore();
}

// 访问图片，保存到相册
export function savePicture() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			//获取用户的当前设置
			success: (res) => {
				if (res.authSetting['scope.writePhotosAlbum']) {
					//验证用户是否授权可以访问相册
					resolve({
						code: 0,
					});
				} else {
					uni.authorize({
						//如果没有授权，向用户发起请求
						scope: 'scope.writePhotosAlbum',
						success: () => {
							resolve({
								code: 0,
							});
						},
						fail: (err) => {
							reject(err);
							uni.showToast({
								title: '请打开保存相册权限，再点击保存相册分享',
								icon: 'none',
								duration: 3000,
							});
							setTimeout(() => {
								uni.openSetting({
									//调起客户端小程序设置界面,让用户开启访问相册
									success: (res2) => {},
								});
							}, 3000);
						},
					});
				}
			},
		});
	});
}

// 字体换行
export function textPrewrap(ctx, content, drawX, drawY, lineHeight, lineMaxWidth, lineNum) {
	var drawTxt = ''; // 当前绘制的内容
	var drawLine = 1; // 第几行开始绘制
	var drawIndex = 0; // 当前绘制内容的索引
	// 判断内容是否可以一行绘制完毕
	if (ctx.measureText(content).width <= lineMaxWidth) {
		ctx.fillText(content.substring(drawIndex, 3), drawX, drawY);
	} else {
		for (var i = 0; i < content.length; i++) {
			drawTxt += content[i];
			if (ctx.measureText(drawTxt).width >= lineMaxWidth) {
				if (drawLine >= lineNum) {
					ctx.fillText(content.substring(drawIndex, i) + '..', drawX, drawY);
					break;
				} else {
					ctx.fillText(content.substring(drawIndex, i + 1), drawX, drawY);
					drawIndex = i + 1;
					drawLine += 1;
					drawY += lineHeight;
					drawTxt = '';
				}
			} else {
				// 内容绘制完毕，但是剩下的内容宽度不到lineMaxWidth
				if (i === content.length - 1) {
					ctx.fillText(content.substring(drawIndex), drawX, drawY);
				}
			}
		}
	}
}

// 下载图片
export function downloadImage(filePath) {
	uni.authorize({
		scope: 'scope.writePhotosAlbum',
		success: () => {
			uni.saveImageToPhotosAlbum({
				filePath: filePath,
				success() {
					uni.hideLoading()
					uni.showToast({
						title: '海报已保存至本地！',
						icon: 'none'
					})
				},
				fail() {
					uni.showToast({
						title: '海报保存失败！',
						icon: 'none'
					})
				}
			})
		},
		fail: () => {
			uni.showModal({
				content: '由于您拒绝保存到您手机里,无法进行保存，点击确定去授权',
				success: (res) => {
					if (res.confirm) {
						uni.openSetting()
					}
				}
			})
		}
	})
}